package com.cip.webapp.action;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cip.admin.model.CIPLogger;
import com.cip.admin.model.Role;
import com.cip.admin.model.User;
import com.cip.admin.service.CIPLoggerService;
import com.cip.admin.service.FriendsService;
import com.cip.admin.service.UserManager;
import com.cip.admin.model.Friends;
import com.cip.common.Constants;
import com.cip.common.util.SecurityUtil;

public class SecurityAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5848317188916742269L;
	
	private CIPLoggerService cipLoggerService;
	
	private FriendsService friendsService;
	
	private UserManager userManager;
	
	private User user;
	
	private List<User> userList;
	
	List<Map<String, String>> listofmap;
	
	List<User> listofSuggests;
	
	
	
	public String postLogin() throws Exception{
		String returnUrl="adminmenu";
		getRequest().getSession().setAttribute("role","ROLE_ADMIN");
		User user = SecurityUtil.getLoggedInUser();
		user.setLoginStatus("Active");		
		userManager.saveOrUpdateUser(user);
		CIPLogger cipLogger=cipLoggerService.getCIPLoggerById(user.getId());
		if(cipLogger == null){
			cipLogger = new CIPLogger();
		}
		cipLogger.setLoginUser(user);
		cipLogger.setTenantId(user.getTenantId());
		cipLogger.setStatus("Active");
		cipLogger.setLogInTime(null);
		cipLogger.setLogOutTime(null);
		cipLoggerService.saveOrUpdateLogger(cipLogger);
		setUserList(friendsService.getAllFriendsById(user.getId()));
		setListofSuggests(friendsService.getSuggestedFriendsList(user));
	    setUser(user);
		Set<Role> roles = user.getRoles();
		for (Role role : roles) {
			if(role.getName().equalsIgnoreCase(Constants.USER_ROLE)){	
				getRequest().getSession().setAttribute("role","ROLE_USER");
				returnUrl="candidatemenu";
				break;
			}
			if(role.getName().equalsIgnoreCase(Constants.ADMIN_SUPER_ROLE)){
				getRequest().getSession().setAttribute("role","ROLE_SUPER_ADMIN");
				returnUrl="superadmin";
				break;
			}

			
		}		
		
		System.out.println("return URL================"+returnUrl);
		
		return returnUrl;
	}	

	public CIPLoggerService getCipLoggerService() {
		return cipLoggerService;
	}

	public void setCipLoggerService(CIPLoggerService cipLoggerService) {
		this.cipLoggerService = cipLoggerService;
	}
	
	public FriendsService getFriendsService() {
		return friendsService;
	}

	public void setFriendsService(FriendsService friendsService) {
		this.friendsService = friendsService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Map<String, String>> getListofmap() {
		return listofmap;
	}

	public void setListofmap(List<Map<String, String>> listofmap) {
		this.listofmap = listofmap;
	}

	public List<User> getListofSuggests() {
		return listofSuggests;
	}

	public void setListofSuggests(List<User> listofSuggests) {
		this.listofSuggests = listofSuggests;
	}
	
	public List<User> getUserList() {
        return userList;
    }
	
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}	
}
